home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / DbaDataStore.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  10.6 KB  |  478 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.util.BitSet;
  4. import symantec.itools.db.pro.RelationView;
  5. import symantec.itools.db.pro.RelationViewMetaData;
  6. import symjava.sql.SQLException;
  7.  
  8. public class DbaDataStore implements DbDataStore, MetaTable, DbDataUpdater {
  9.    RelationView master;
  10.    RelationView relView;
  11.    DbDataSource source;
  12.    boolean manRowChangeFlag = false;
  13.    DbaDataLink link;
  14.    RelationViewMetaData meta;
  15.    public static final int MAX_DISPLAY_SIZE = 60;
  16.    int currRecord;
  17.    int[] rowMapping;
  18.    BitSet createdRows;
  19.    int lastValidIndex;
  20.    int lastIndex_;
  21.    int lastRowRetrieved;
  22.    boolean obtainedAllRows = false;
  23.    static final int START_SIZE = 100;
  24.    static final int INC_SIZE = 100;
  25.  
  26.    public DbaDataStore(RelationView rv, RelationView masterView) throws SQLException {
  27.       if (masterView != null) {
  28.          this.master = masterView;
  29.       }
  30.  
  31.       this.relView = rv;
  32.       this.meta = this.relView.getMetaData();
  33.       this.initRowMapping();
  34.    }
  35.  
  36.    void changeInMaster() {
  37.       if (!this.manualRowChangeFlag()) {
  38.          this.source.view.clear();
  39.          this.source.view.forceRedraw(true);
  40.       }
  41.  
  42.    }
  43.  
  44.    public boolean manualRowChangeFlag() {
  45.       return this.manRowChangeFlag;
  46.    }
  47.  
  48.    void setManRowChangeFlag(boolean val) {
  49.       if (this.manRowChangeFlag != val) {
  50.          this.manRowChangeFlag = val;
  51.  
  52.          try {
  53.             if (val) {
  54.                this.currRecord = this.relView.getCurrentRecordNumber();
  55.             } else {
  56.                this.relView.goTo(this.currRecord);
  57.             }
  58.  
  59.             this.relView.enableBindingNotify(!val, true);
  60.             this.relView.enableDetailSQL(!val);
  61.          } catch (SQLException var2) {
  62.          }
  63.       }
  64.    }
  65.  
  66.    public void setCurrentRow(int row) throws TypeNotSupported {
  67.       ++row;
  68.  
  69.       try {
  70.          this.relView.goTo(this.translateRow(row));
  71.       } catch (Exception ex) {
  72.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  73.       }
  74.    }
  75.  
  76.    public void setDbDataSource(DbDataSource ds) {
  77.       this.source = ds;
  78.       this.link = new DbaDataLink(this);
  79.       this.setManRowChangeFlag(true);
  80.  
  81.       try {
  82.          if (this.master != null) {
  83.             this.master.bindCurrentRecord(this.link);
  84.          }
  85.       } catch (SQLException var2) {
  86.       }
  87.  
  88.       this.setManRowChangeFlag(false);
  89.    }
  90.  
  91.    public boolean supportsCaching() {
  92.       return true;
  93.    }
  94.  
  95.    public synchronized int validDataRowRange(int top, int bottom) throws DataNotAvailable {
  96.       ++top;
  97.       bottom += 2;
  98.       int lastInRange = Math.max(1, top);
  99.  
  100.       try {
  101.          this.setManRowChangeFlag(true);
  102.  
  103.          while(lastInRange <= bottom) {
  104.             this.translateRow(lastInRange);
  105.             ++lastInRange;
  106.          }
  107.       } catch (DataNotAvailable var8) {
  108.       } finally {
  109.          this.setManRowChangeFlag(false);
  110.       }
  111.  
  112.       if (lastInRange == top) {
  113.          throw new DataNotAvailable("top is greater than last row in database");
  114.       } else {
  115.          return lastInRange - 1;
  116.       }
  117.    }
  118.  
  119.    public synchronized Data getData(int row, int col) throws DataNotAvailable {
  120.       ++row;
  121.       Data d = null;
  122.  
  123.       try {
  124.          this.setManRowChangeFlag(true);
  125.          if (!this.relView.goTo(this.translateRow(row))) {
  126.             throw new DataNotAvailable("Could not move to row " + row);
  127.          }
  128.  
  129.          String str = this.relView.getStringValue(col);
  130.          if (str == null) {
  131.             str = "";
  132.          }
  133.  
  134.          d = new ImageStringData(this.source, str);
  135.       } catch (Exception ex) {
  136.          throw new DataNotAvailable(((Throwable)ex).getMessage());
  137.       } finally {
  138.          this.setManRowChangeFlag(false);
  139.       }
  140.  
  141.       return d;
  142.    }
  143.  
  144.    public synchronized void update(int row, int col, Data data) throws TypeNotSupported {
  145.       ++row;
  146.  
  147.       try {
  148.          this.setManRowChangeFlag(true);
  149.          this.relView.goTo(this.translateRow(row));
  150.          this.relView.setValueFromString(col, data.toString());
  151.       } catch (Exception ex) {
  152.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  153.       } finally {
  154.          this.setManRowChangeFlag(false);
  155.       }
  156.  
  157.    }
  158.  
  159.    public int rowState(int row) {
  160.       ++row;
  161.       int rowState = 0;
  162.  
  163.       try {
  164.          this.setManRowChangeFlag(true);
  165.          this.relView.goTo(this.translateRow(row));
  166.          byte state = this.relView.getCurrentRecordState();
  167.          switch (state) {
  168.             case 100:
  169.                rowState = 1;
  170.                break;
  171.             case 101:
  172.                rowState = 3;
  173.                break;
  174.             case 102:
  175.                rowState = 0;
  176.                break;
  177.             case 103:
  178.                rowState = 2;
  179.          }
  180.       } catch (Exception ex) {
  181.          ((Throwable)ex).printStackTrace();
  182.       } finally {
  183.          this.setManRowChangeFlag(false);
  184.       }
  185.  
  186.       return rowState;
  187.    }
  188.  
  189.    public void clear() {
  190.       this.initRowMapping();
  191.    }
  192.  
  193.    public void refresh() {
  194.       try {
  195.          this.setManRowChangeFlag(true);
  196.          this.relView.restartMultiView();
  197.          this.setManRowChangeFlag(false);
  198.          this.initRowMapping();
  199.       } catch (SQLException var5) {
  200.       } finally {
  201.          this.setManRowChangeFlag(false);
  202.       }
  203.  
  204.    }
  205.  
  206.    public void undoRow(int row) throws TypeNotSupported {
  207.       ++row;
  208.  
  209.       try {
  210.          this.setManRowChangeFlag(true);
  211.          this.relView.goTo(this.translateRow(row));
  212.          this.relView.undoRecord();
  213.       } catch (SQLException ex) {
  214.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  215.       } catch (DataNotAvailable ex) {
  216.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  217.       } finally {
  218.          this.setManRowChangeFlag(false);
  219.       }
  220.  
  221.    }
  222.  
  223.    public int rowsRetrieved() {
  224.       return this.lastIndex_;
  225.    }
  226.  
  227.    public void undeleteRow(int row) throws TypeNotSupported {
  228.       try {
  229.          ++row;
  230.          this.setManRowChangeFlag(true);
  231.          if (this.relView.goTo(this.translateRow(row))) {
  232.             this.relView.undoRecord();
  233.          }
  234.       } catch (SQLException ex) {
  235.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  236.       } catch (DataNotAvailable ex) {
  237.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  238.       } finally {
  239.          this.setManRowChangeFlag(false);
  240.       }
  241.  
  242.    }
  243.  
  244.    public synchronized void deleteRow(int row) throws TypeNotSupported {
  245.       try {
  246.          ++row;
  247.          this.setManRowChangeFlag(true);
  248.          if (this.relView.goTo(this.translateRow(row))) {
  249.             this.relView.deleteRecord();
  250.          }
  251.       } catch (SQLException ex) {
  252.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  253.       } catch (DataNotAvailable ex) {
  254.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  255.       } finally {
  256.          this.setManRowChangeFlag(false);
  257.       }
  258.  
  259.    }
  260.  
  261.    public void save() throws TypeNotSupported {
  262.       try {
  263.          this.relView.saveMultiView();
  264.          this.refresh();
  265.       } catch (SQLException ex) {
  266.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  267.       }
  268.    }
  269.  
  270.    public synchronized void insertRow(int row) throws TypeNotSupported {
  271.       ++row;
  272.  
  273.       try {
  274.          this.setManRowChangeFlag(true);
  275.          this.insertRowIntoMap(row);
  276.       } catch (DataNotAvailable ex) {
  277.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  278.       } finally {
  279.          this.setManRowChangeFlag(false);
  280.       }
  281.  
  282.    }
  283.  
  284.    public synchronized int appendRow() throws TypeNotSupported {
  285.       int ex;
  286.       try {
  287.          this.setManRowChangeFlag(true);
  288.          ex = this.appendRowIntoMap();
  289.       } catch (DataNotAvailable ex) {
  290.          throw new TypeNotSupported(((Throwable)ex).getMessage());
  291.       } finally {
  292.          this.setManRowChangeFlag(false);
  293.       }
  294.  
  295.       return ex;
  296.    }
  297.  
  298.    public void setupGrid(Grid view) {
  299.       try {
  300.          int cols = this.meta.getColumnCount();
  301.          view.createColumns(cols);
  302.          view.setRowLabelHeadingStyle(0);
  303.  
  304.          for(int col = 1; col <= cols; ++col) {
  305.             int size = this.meta.getColumnDisplaySize(col);
  306.             size = Math.min(0, size);
  307.             size = Math.max(0, 60);
  308.             view.setHeading(this.meta.getColumnLabel(col), col, Math.min(size, 10));
  309.             this.setupColumn(view, col);
  310.          }
  311.  
  312.       } catch (SQLException var5) {
  313.       }
  314.    }
  315.  
  316.    public void setDataSource(DataSource ds) {
  317.    }
  318.  
  319.    public synchronized boolean isDataEditable(int row, int col) throws DataNotAvailable {
  320.       boolean ex;
  321.       try {
  322.          ++row;
  323.          this.setManRowChangeFlag(true);
  324.          if (!this.relView.goTo(this.translateRow(row))) {
  325.             return true;
  326.          }
  327.  
  328.          ex = true;
  329.       } catch (SQLException ex) {
  330.          throw new DataNotAvailable(((Throwable)ex).getMessage());
  331.       } finally {
  332.          this.setManRowChangeFlag(false);
  333.       }
  334.  
  335.       return ex;
  336.    }
  337.  
  338.    public Data[] arrangeForViewing(Data[] data) {
  339.       return data;
  340.    }
  341.  
  342.    public void setupColumn(Grid view, int col) {
  343.       try {
  344.          int type = this.meta.getColumnType(col);
  345.          switch (type) {
  346.             case -7:
  347.             case -4:
  348.             case -3:
  349.             case -2:
  350.             case -1:
  351.             case 0:
  352.             case 1:
  353.             case 12:
  354.             case 91:
  355.             case 92:
  356.             case 93:
  357.             case 1111:
  358.             default:
  359.                view.setColumnAlignment(col, 0);
  360.                break;
  361.             case -6:
  362.             case -5:
  363.             case 2:
  364.             case 4:
  365.             case 5:
  366.             case 6:
  367.             case 7:
  368.             case 8:
  369.                view.setColumnAlignment(col, 2);
  370.          }
  371.  
  372.          if (this.meta.isCurrency(col)) {
  373.             view.setColumnAlignment(col, 2);
  374.          }
  375.  
  376.          if (this.meta.isWritable(col)) {
  377.             view.setColEditable(col, false);
  378.             return;
  379.          }
  380.       } catch (SQLException var4) {
  381.          view.setColumnAlignment(col, 0);
  382.       }
  383.  
  384.    }
  385.  
  386.    void initRowMapping() {
  387.       this.rowMapping = new int[100];
  388.       this.lastValidIndex = 99;
  389.       this.lastRowRetrieved = 0;
  390.       this.lastIndex_ = 0;
  391.       this.obtainedAllRows = false;
  392.       this.createdRows = new BitSet();
  393.    }
  394.  
  395.    void printMap() {
  396.       System.out.println("Row mappings - size=" + this.lastValidIndex + "\n\tlastIndex=" + this.lastIndex_ + "  lastRow=" + this.lastRowRetrieved);
  397.  
  398.       for(int i = 0; i <= this.lastIndex_; ++i) {
  399.          System.out.println("\trow " + i + " -> " + this.rowMapping[i]);
  400.       }
  401.  
  402.    }
  403.  
  404.    int translateRow(int r) throws DataNotAvailable {
  405.       try {
  406.          while(this.lastIndex_ < r) {
  407.             if (this.createdRows.get(this.lastRowRetrieved + 1)) {
  408.                ++this.lastRowRetrieved;
  409.             } else {
  410.                if (!this.relView.goTo(this.lastRowRetrieved + 1)) {
  411.                   this.obtainedAllRows = true;
  412.                   throw new DataNotAvailable("Ran out of rows to fetch at row=" + (this.lastRowRetrieved + 1));
  413.                }
  414.  
  415.                this.lastRowRetrieved = this.relView.getCurrentRecordNumber();
  416.                ++this.lastIndex_;
  417.                this.growMap();
  418.                this.rowMapping[this.lastIndex_] = this.lastRowRetrieved;
  419.             }
  420.          }
  421.  
  422.          return this.rowMapping[r];
  423.       } catch (SQLException e) {
  424.          throw new DataNotAvailable(((Throwable)e).getMessage());
  425.       }
  426.    }
  427.  
  428.    int insertRowIntoMap(int r) throws DataNotAvailable {
  429.       try {
  430.          this.translateRow(r);
  431.          this.relView.getNewRecord();
  432.          int actual = this.relView.getCurrentRecordNumber();
  433.          this.createdRows.set(actual);
  434.          this.growMap();
  435.          int numToCopy = this.lastIndex_ + 1 - r;
  436.          System.arraycopy(this.rowMapping, r, this.rowMapping, r + 1, numToCopy);
  437.          ++this.lastIndex_;
  438.          this.rowMapping[r] = actual;
  439.          return actual;
  440.       } catch (Exception e) {
  441.          throw new DataNotAvailable(((Throwable)e).getMessage());
  442.       }
  443.    }
  444.  
  445.    int appendRowIntoMap() throws DataNotAvailable {
  446.       try {
  447.          while(!this.obtainedAllRows) {
  448.             this.translateRow(this.lastRowRetrieved + 1);
  449.          }
  450.       } catch (DataNotAvailable ex) {
  451.          if (!this.obtainedAllRows) {
  452.             throw ex;
  453.          }
  454.       }
  455.  
  456.       try {
  457.          this.relView.getNewRecord();
  458.          this.growMap();
  459.          this.lastRowRetrieved = this.relView.getCurrentRecordNumber();
  460.          this.createdRows.set(this.lastRowRetrieved);
  461.          this.rowMapping[++this.lastIndex_] = this.lastRowRetrieved;
  462.       } catch (SQLException var2) {
  463.          throw new DataNotAvailable("Could not create new record");
  464.       }
  465.  
  466.       return this.lastIndex_;
  467.    }
  468.  
  469.    final void growMap() {
  470.       if (this.lastIndex_ == this.lastValidIndex - 1) {
  471.          int[] newMap = new int[this.lastValidIndex += 100];
  472.          System.arraycopy(this.rowMapping, 0, newMap, 0, this.rowMapping.length);
  473.          this.rowMapping = newMap;
  474.       }
  475.  
  476.    }
  477. }
  478.